﻿// Decompiled with JetBrains decompiler
// Type: Commom.Util.ConvertUtil
// Assembly: MT5Trade, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: 8045A2CB-DBD6-41AD-8804-F98CAC1BDC8D
// Assembly location: C:\projects\trade\trade_tool\trade0812\MT5Trade.dll

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

#nullable enable
namespace Commom.Util;

public static class ConvertUtil
{
  public static int ToInt(object input) => ConvertUtil.ToIntOrNull(input).GetValueOrDefault();

  public static int? ToIntOrNull(object input)
  {
    int result;
    if (int.TryParse(input.SafeString(), out result))
      return new int?(result);
    try
    {
      double? doubleOrNull = ConvertUtil.ToDoubleOrNull(input, new int?(0));
      return !doubleOrNull.HasValue ? new int?() : new int?(Convert.ToInt32((object) doubleOrNull));
    }
    catch
    {
      return new int?();
    }
  }

  public static long ToLong(object input) => ConvertUtil.ToLongOrNull(input).GetValueOrDefault();

  public static long? ToLongOrNull(object input)
  {
    long result;
    if (long.TryParse(input.SafeString(), out result))
      return new long?(result);
    try
    {
      Decimal? decimalOrNull = ConvertUtil.ToDecimalOrNull(input, new int?(0));
      return !decimalOrNull.HasValue ? new long?() : new long?(Convert.ToInt64((object) decimalOrNull));
    }
    catch
    {
      return new long?();
    }
  }

  public static float ToFloat(object input, int? digits = null)
  {
    return ConvertUtil.ToFloatOrNull(input, digits).GetValueOrDefault();
  }

  public static float? ToFloatOrNull(object input, int? digits = null)
  {
    float result;
    if (!float.TryParse(input.SafeString(), out result))
      return new float?();
    return !digits.HasValue ? new float?(result) : new float?((float) Math.Round((double) result, digits.Value));
  }

  public static double ToDouble(object input, int? digits = null)
  {
    return ConvertUtil.ToDoubleOrNull(input, digits).GetValueOrDefault();
  }

  public static double? ToDoubleOrNull(object input, int? digits = null)
  {
    double result;
    if (!double.TryParse(input.SafeString(), out result))
      return new double?();
    return !digits.HasValue ? new double?(result) : new double?(Math.Round(result, digits.Value));
  }

  public static Decimal ToDecimal(object input, int? digits = null)
  {
    return ConvertUtil.ToDecimalOrNull(input, digits).GetValueOrDefault();
  }

  public static Decimal? ToDecimalOrNull(object input, int? digits = null)
  {
    Decimal result;
    if (!Decimal.TryParse(input.SafeString(), out result))
      return new Decimal?();
    return !digits.HasValue ? new Decimal?(result) : new Decimal?(Math.Round(result, digits.Value));
  }

  public static bool ToBool(object input) => ConvertUtil.ToBoolOrNull(input).GetValueOrDefault();

  public static bool? ToBoolOrNull(object input)
  {
    bool? nullable = ConvertUtil.GetBool(input);
    if (nullable.HasValue)
      return new bool?(nullable.Value);
    bool result;
    return !bool.TryParse(input.SafeString(), out result) ? new bool?() : new bool?(result);
  }

  private static bool? GetBool(object input)
  {
    string lower = input.SafeString().ToLower();
    if (lower != null)
    {
      switch (lower.Length)
      {
        case 1:
          switch (lower[0])
          {
            case '0':
              return new bool?(false);
            case '1':
              return new bool?(true);
            case '不':
              return new bool?(false);
            case '否':
              return new bool?(false);
            case '是':
              return new bool?(true);
          }
          break;
        case 2:
          switch (lower[0])
          {
            case 'n':
              if (lower == "no")
                return new bool?(false);
              break;
            case 'o':
              if (lower == "ok")
                return new bool?(true);
              break;
          }
          break;
        case 3:
          if (lower == "yes")
            return new bool?(true);
          break;
        case 4:
          if (lower == "fail")
            return new bool?(false);
          break;
      }
    }
    return new bool?();
  }

  public static DateTime ToDate(object input)
  {
    return ConvertUtil.ToDateOrNull(input) ?? DateTime.MinValue;
  }

  public static DateTime? ToDateOrNull(object input)
  {
    DateTime result;
    return !DateTime.TryParse(input.SafeString(), out result) ? new DateTime?() : new DateTime?(result);
  }

  public static Guid ToGuid(object input) => ConvertUtil.ToGuidOrNull(input) ?? Guid.Empty;

  public static Guid? ToGuidOrNull(object input)
  {
    Guid result;
    return !Guid.TryParse(input.SafeString(), out result) ? new Guid?() : new Guid?(result);
  }

  public static List<Guid> ToGuidList(string input) => ConvertUtil.ToList<Guid>(input);

  public static List<T> ToList<T>(string input)
  {
    List<T> list = new List<T>();
    if (string.IsNullOrWhiteSpace(input))
      return list;
    string[] source = input.Split(',');
    list.AddRange(((IEnumerable<string>) source).Where<string>((Func<string, bool>) (each => !string.IsNullOrWhiteSpace(each))).Select<string, T>((Func<string, T>) (each => ConvertUtil.To<T>((object) each))));
    return list;
  }

  public static T To<T>(object input)
  {
    if (input == null)
      return default (T);
    if (input is string && string.IsNullOrWhiteSpace(input.ToString()))
      return default (T);
    Type type = Common.GetType<T>();
    string lower = type.Name.ToLower();
    try
    {
      switch (lower)
      {
        case "string":
          return (T) (object) input.ToString();
        case "guid":
          return (T) (object) new Guid(input.ToString());
        default:
          if (type.IsEnum)
            return EnumUtil.Parse<T>(input);
          return input is IConvertible ? (T) Convert.ChangeType(input, type) : (T) input;
      }
    }
    catch
    {
      return default (T);
    }
  }

  public static byte[] ToBytes(string input) => ConvertUtil.ToBytes(input, Encoding.UTF8);

  public static byte[] ToBytes(string input, Encoding encoding)
  {
    return !string.IsNullOrWhiteSpace(input) ? encoding.GetBytes(input) : new byte[0];
  }
}
